<script lang="ts" setup>
import { computed } from "vue"
import { reactive, ref } from "vue"
import { useRouter } from "vue-router"
import { useUserStore } from "@/store/modules/user"
import { type FormInstance, type FormRules } from "element-plus"
import { User, Lock, Key, Picture, Loading } from "@element-plus/icons-vue"
import { getCssVariableValue } from "@/utils"
// import { getLoginCodeApi } from "@/api/login"
import { type LoginRequestData } from "@/api/login/types/login"
import ThemeSwitch from "@/components/ThemeSwitch/index.vue"

const mcolor = getCssVariableValue("--mcolor")
const router = useRouter()

/** 登录表单元素的引用 */
const loginFormRef = ref<FormInstance | null>(null)

/** 登录按钮 Loading */
const loading = ref(false)
const ispwd = ref(false)
/** 验证码图片 URL */
const baseCodeUrl = "api/web/verify/code/image?id="
const codeUrl = ref("")
/** 登录表单数据 */
const loginFormData: LoginRequestData = reactive({
  account: "",
  pwd: "",
  code: ""
})
/** 登录表单校验规则 */
const loginFormRules: FormRules = {
  account: [{ required: true, message: "请输入用户名", trigger: "blur" }],
  pwd: [
    { required: true, message: "请输入密码", trigger: "blur" },
    { min: 1, max: 16, message: "长度在 1 到 16 个字符", trigger: "blur" }
  ],
  code: [{ required: true, message: "请输入验证码", trigger: "blur" }]
}
/** 登录逻辑 */
const handleLogin = () => {
  loginFormRef.value?.validate((valid: boolean, fields) => {
    if (valid) {
      loading.value = true
      useUserStore()
        .login(loginFormData)
        .then(() => {
          router.push({ path: "/electric/device/gateway" })
        })
        .catch(() => {
          createCode()
          loginFormData.pwd = ""
        })
        .finally(() => {
          loading.value = false
        })
    } else {
      console.error("表单校验不通过", fields)
    }
  })
}
const createCode = () => {
  codeUrl.value = Math.random().toString(16).slice(2)
}


/** 初始化验证码 */
createCode()
</script>

<template>
  <div class="login-container">
    <ThemeSwitch class="theme-switch" />
    <div class="login-card">
      <div class="title">
        集云测试项目平台用户端
      </div>
      <div class="content" v-if="!ispwd">
        <el-form ref="loginFormRef" :model="loginFormData" :rules="loginFormRules" @keyup.enter="handleLogin">
          <el-form-item prop="account">
            <el-input v-model.trim="loginFormData.account" placeholder="用户名" type="text" tabindex="1"
                      :prefix-icon="User" size="large" />
          </el-form-item>
          <el-form-item prop="pwd">
            <el-input v-model.trim="loginFormData.pwd" placeholder="密码" type="password" tabindex="2" :prefix-icon="Lock"
                      size="large" show-password />
          </el-form-item>
          <!-- <el-form-item prop="code">
            <div class="flexCenter">
              <el-input v-model.trim="loginFormData.code" placeholder="验证码" type="text" tabindex="3" :prefix-icon="Key"
                        maxlength="7" size="large">
              </el-input>
              <el-image class="codeimage ml10" :src="baseCodeUrl + codeUrl" @click="createCode" draggable="false">
                <template #placeholder>
                  <el-icon>
                    <Picture />
                  </el-icon>
                </template>
<template #error>
                  <el-icon>
                    <Loading />
                  </el-icon>
                </template>
</el-image>
</div>

</el-form-item> -->
          <div class="flexCenter pt10">
            <el-button type="text" @click="ispwd = !ispwd">忘记密码</el-button>
            <div class="flex1"></div>
            <el-button :loading="loading" style="border-radius: 10px;" type="primary" @click.prevent="handleLogin">登
              录</el-button>
          </div>
        </el-form>
      </div>
      <div v-else class="content columnCenter">
        <div class="font20 bold color1">请联系管理员重置密码</div>
        <el-button class="mt20" type="primary" @click="ispwd = false">返回登录</el-button>
      </div>
    </div>
  </div>
</template>

<style lang="scss" scoped>
@import "@/styles/mixins.scss";

.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  width: 100%;
  min-height: 100%;

  .theme-switch {
    position: fixed;
    top: 5%;
    right: 5%;
    cursor: pointer;
  }

  .login-card {
    width: 380px;
    border-radius: 10px;
    box-shadow: 0 0 10px #ddd;
    background-color: #fff;
    border: 1px solid transparent;
    border-color: var(--mcolor);
    overflow: hidden;

    .title {
      display: flex;
      justify-content: center;
      align-items: center;
      height: 80px;
      font-size: 24px;
      background-color: var(--mcolor);
      color: var(--mcolorText);

      img {
        height: 100%;
      }
    }

    .content {
      padding: 20px 50px;
      height: 256px;

      .codeimage {
        width: 100px;
        height: 35px;
        overflow: hidden;
        border-radius: 10px;
        border-left: 0px;
        user-select: none;
        cursor: pointer;
        text-align: center;
      }

    }
  }
}
</style>
